const plugins = require.context('./tinymce/plugins', true, /\.js$/);
export default class editorConfig {
	constructor(target, callback) {
		import('./tinymce/skins/content/default/content.min.css');
		//加载皮肤
		import('./tinymce/skins/ui/oxide/skin.min.css');
		//导入图标
		import('./tinymce/icons/default/icons.min.js');
		//加载插件
		plugins.keys().forEach(path => {
			import(`./tinymce/plugins${path.slice(1)}`);
		});
		this.#initializeEditor(target, callback);
	}

	#initializeEditor(target, callback) {
		window.tinymce.init({
			target,
			height: 500,
			menubar: false,
			language: 'zh_CN',
			language_url: './tinymce/langs/zh_CN.js',
			theme_url: './tinymce/themes/silver/theme.min.js',
			content_css: './tinymce/skins/ui/oxide/content.min.css',
			plugins: [
				'table advlist autolink lists link charmap print preview hr anchor pagebreak',
				'searchreplace wordcount visualblocks visualchars code fullscreen',
				'insertdatetime nonbreaking save table contextmenu directionality',
				'emoticons textcolor colorpicker textpattern image imagetools code codesample toc'
			],
			toolbar: 'styleselect | bold italic underline | alignleft aligncenter alignright alignjustify | lineheight removeformat | table outdent indent',
			fontsize_formats: '11px 12px 14px 16px 18px 24px 36px 48px',
			font_formats: '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif',
			setup(editor) {
				editor.on('input change', () => {
					if (!!callback && typeof callback === 'function') {
						callback(editor.getContent());
					}
				});
			},
			image_advtab: true,
			//images_upload_url
			paste_data_images: true,
			automatic_uploads: true,
			media_live_embeds: true, //查看上传的视频
			table_style_by_css: true,
			table_responsive_width: 'none', //表格自适应
			//图片选择上传
			images_upload_handler: this.handleImageUpload
		});
	}

	handleImageUpload(blobInfo, success, failure) {
		console.log(blobInfo, success, failure);
		success('123123');
	}
}